using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using WebControls;
using EntityLayer;
using BusinessLayer;
using InfraStructureLayer;

public partial class blog_entryDetail : SFPage
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public override void LanguageLoad()
    {
        if (this.QueryStringId.ToString() == "")
        {
            if (Request.QueryString["PermalinkGuid"] != null && Request.QueryString["PermalinkGuid"] != "")
            {
                this.QueryStringId = Request.QueryString["PermalinkGuid"];
            }
        }
        if (this.QueryStringId != "")
        {
            this.Master.FindControl("ContentPlaceHolderMain").Visible = true;
            entEntryContents insEntEntryContents = new entEntryContents();
            insEntEntryContents.PermalinkGUID = new Guid(this.QueryStringId);
            busEntryContents insBusEnrtyContents = new busEntryContents();
            insBusEnrtyContents.SelectEntryContentByPermalinkGUID(insEntEntryContents);
            if (insBusEnrtyContents.HasErrors)
            {
                MessageBoxShow(insBusEnrtyContents);
                this.Master.FindControl("ContentPlaceHolderMain").Visible = false;
                return;
            }

            entEntries insEntEntries = new entEntries();
            insEntEntries.EntryId = insEntEntryContents.EntryId;
            busEntries insBusEntries = new busEntries();
            insBusEntries.SelectEntriesByEntryId(insEntEntries);
            if (insBusEntries.HasErrors)
            {
                MessageBoxShow(insBusEntries);
                this.Master.FindControl("ContentPlaceHolderMain").Visible = false;
                return;
            }

            DataTable dt = insBusEnrtyContents.SelectEntryContentsByEntryId(insEntEntryContents);
            if (dt.Select("LanguageId=" + CurrentContext.LanguageId.Value.ToString() + " AND PermalinkGUID='" + this.QueryStringId + "'").Length == 0)
            {
                if (dt.Select("LanguageId=" + CurrentContext.LanguageId.Value.ToString())[0]["PermalinkGUID"] == null || dt.Select("LanguageId=" + CurrentContext.LanguageId.Value.ToString())[0]["PermalinkGUID"] == DBNull.Value || dt.Select("LanguageId=" + CurrentContext.LanguageId.Value.ToString())[0]["PermalinkGUID"].ToString() == "")
                {
                    MessageBoxShow(BusinessBase.GetErrorMessageMultilanguageString(43, new object[] { CurrentContext.Languages.Select("LanguageId=" + CurrentContext.LanguageId.ToString())[0]["LanguageName"].ToString() }));
                    this.Master.FindControl("ContentPlaceHolderMain").Visible = false;
                    return;
                }
                else
                {
                    Response.Redirect("entryDetail.aspx?PermalinkGuid=" + dt.Select("LanguageId=" + CurrentContext.LanguageId.Value.ToString())[0]["PermalinkGUID"].ToString());
                }
            }


            lblBody.Text = insEntEntryContents.Body;
            lblDate.Text = insEntEntries.PublishDate.ToShortDateString();
            lblTitle.Text = insEntEntryContents.Title;
            this.Title = insEntEntryContents.Title;
            entEntryCategories insEntEntryCategories = new entEntryCategories();
            busEntryCategories insBusEntryCategories = new busEntryCategories();
            insEntEntryCategories.EntryId = insEntEntryContents.EntryId;
            DataTable insDataTableEntryCategories = insBusEntryCategories.SelectEntryCategoriesByEntryIdAndLanguageId(insEntEntryCategories);
            if (insBusEntryCategories.HasErrors)
            {
                MessageBoxShow(insBusEntryCategories);
                this.Master.FindControl("ContentPlaceHolderMain").Visible = false;
                return;
            }

            string s = "";
            foreach (DataRow dr in insDataTableEntryCategories.Rows)
            {
                s += dr["CategoryName"].ToString() + ",";
            }
            if (s.EndsWith(","))
            {
                s = s.Substring(0, s.Length - 1);
            }
            lblCategories.Text = s;

            entComments insEntComments = new entComments();
            busComments insBusComments = new busComments();
            insEntComments.EntryContentId = insEntEntryContents.EntryContentId;
            DataTable insDataTableComments = insBusComments.SelectCommentsByEntryContentId(insEntComments);
            if (insBusComments.HasErrors)
            {
                MessageBoxShow(insBusComments);
                this.Master.FindControl("ContentPlaceHolderMain").Visible = false;
                return;
            }
            sfrComments.DataSource = insDataTableComments;
            sfrComments.DataBind();
        }
        base.LanguageLoad();
    }
    protected void sfrComments_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "DeleteComment")
        {
            entComments insEntComments = new entComments();
            insEntComments.CommentId = Convert.ToInt32(e.CommandArgument);
            busComments insBusComments = new busComments();
            insBusComments.DeleteCommentsByCommentId(insEntComments);
            if (insBusComments.HasErrors)
            {
                MessageBoxShow(insBusComments);
                return;
            }

            entEntryContents insEntEntryContents = new entEntryContents();
            insEntEntryContents.PermalinkGUID = new Guid(this.QueryStringId);
            busEntryContents insBusEnrtyContents = new busEntryContents();
            insBusEnrtyContents.SelectEntryContentByPermalinkGUID(insEntEntryContents);
            if (insBusEnrtyContents.HasErrors)
            {
                MessageBoxShow(insBusEnrtyContents);
                return;
            }


            insEntComments.EntryContentId = insEntEntryContents.EntryContentId;
            DataTable insDataTableComments = insBusComments.SelectCommentsByEntryContentId(insEntComments);
            if (insBusComments.HasErrors)
            {
                MessageBoxShow(insBusComments);
                return;
            }
            sfrComments.DataSource = insDataTableComments;
            sfrComments.DataBind();
        }
    }
    public override void Save()
    {
        Validate();
        if (this.HasErrors)
        {
            MessageBoxShow(this);
            return;
        }
        if (Session["SecurityPicture"].ToString() != txtSecurityPicture.Text)
        {
            MessageBoxShow(StaticMethods.GetErrorMessageMultilanguageString(45, new object[] { }));
            return;
        }
        if (txtCommentUserEmail.Text.Trim().Length > 0)
        {
            if (!WebControls.Validation.ValidateEmail(txtCommentUserEmail.Text))
            {
                MessageBoxShow(StaticMethods.GetErrorMessageMultilanguageString(18, new object[] { lblCommentUserEmail.Text }));
                return;
            }
        }
        if (txtCommentUserWebSite.Text.Trim().Length > 0)
        {
            if (!WebControls.Validation.ValidateWebSite(txtCommentUserWebSite.Text))
            {
                MessageBoxShow(StaticMethods.GetErrorMessageMultilanguageString(18, new object[] { lblCommentUserWebSite.Text }));
                return;
            }
        }

        entComments insEntComments = new entComments();
        insEntComments.CommentBody = txtCommentBody.Text;
        insEntComments.CommentDate = DateTime.Now;
        insEntComments.CommentUserEMail = txtCommentUserEmail.Text;
        insEntComments.CommentUserIPAddress = Request.UserHostAddress;
        insEntComments.CommentUserName = txtCommentUserName.Text;
        insEntComments.CommentUserWebSite = txtCommentUserWebSite.Text;


        entEntryContents insEntEntryContents = new entEntryContents();
        insEntEntryContents.PermalinkGUID = new Guid(this.QueryStringId);
        busEntryContents insBusEnrtyContents = new busEntryContents();
        insBusEnrtyContents.SelectEntryContentByPermalinkGUID(insEntEntryContents);
        if (insBusEnrtyContents.HasErrors)
        {
            MessageBoxShow(insBusEnrtyContents);
            return;
        }

        insEntComments.EntryContentId = insEntEntryContents.EntryContentId;
        busComments insBusComments = new busComments();
        insBusComments.InsertComments(insEntComments);
        if (insBusComments.HasErrors)
        {
            MessageBoxShow(insBusComments);
            return;
        }
        base.SaveNew();

        DataTable insDataTableComments = insBusComments.SelectCommentsByEntryContentId(insEntComments);
        if (insBusComments.HasErrors)
        {
            MessageBoxShow(insBusComments);
            return;
        }
        Session["SecurityPicture"] = null;
        txtCommentBody.Text = "";
        insEntComments.CommentUserEMail = "";
        txtCommentUserName.Text="";
        txtCommentUserWebSite.Text="";
        txtSecurityPicture.Text = "";
        sfrComments.DataSource = insDataTableComments;
        sfrComments.DataBind();

    }
    protected bool GetVisible()
    {
        return (CurrentContext.UserId != null);
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        Save();
    }
}
